﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Controls.Ribbon;
using Tanmia.Domain;
using Tanmia.Core;
using Tanmia.Controls;

namespace Tanmia.Windows.PersonScenario
{
    /// <summary>
    /// Interaction logic for PersonEdit.xaml
    /// </summary>
    public partial class PersonEdit : RibbonWindow
    {
        private AccountDomain accountDomainObj = new AccountDomain();
        private PersonViewModel PersonVM = new PersonViewModel();

        public PersonEdit()
        {
            InitializeComponent();

            DataContext = PersonVM;
        }


        private void ShowValidationTool(string error)
        {
            vatError.Label = error;
            vatError.Visibility = Visibility.Visible;
        }

        private void SetAccount()
        {
            if (cmbAccount.SelectedItem == Account.NewAccount)
                PersonVM.Entity.Account = CreateNewAccount();
            else if (cmbAccount.SelectedItem == Account.NullAccount)
                PersonVM.Entity.Account = null;
        }

        private void UpdateAccounts()
        {
            if (cmbAccount.SelectedItem == Account.NewAccount)
            {
                PersonVM.UpdateAccounts();

                cmbAccount.SelectedIndex = cmbAccount.Items.Count - 1;
            }
            else if (cmbAccount.SelectedItem == Account.NullAccount || cmbAccount.SelectedItem == null)
            {
                cmbAccount.SelectedIndex = 2;
            }
        }

        private Account CreateNewAccount()
        {
            if (cmbPersonType.SelectedIndex == 0)
                return accountDomainObj.SaveOrUpdate(new Account() { CreateDate = DateTime.Now, Name = txtPersonName.Text, Note = txtNote.Text, Number = accountDomainObj.GetMaxAccount(accountDomainObj.GetByNumber(Account.CustomersAccount)), Parent = accountDomainObj.GetByNumber(Account.CustomersAccount) });
            else if (cmbPersonType.SelectedIndex == 1)
                return accountDomainObj.SaveOrUpdate(new Account() { CreateDate = DateTime.Now, Name = txtPersonName.Text, Note = txtNote.Text, Number = accountDomainObj.GetMaxAccount(accountDomainObj.GetByNumber(Account.MerchantsAccount)), Parent = accountDomainObj.GetByNumber(Account.MerchantsAccount) });

            return null;
        }

        private bool Save()
        {
            PersonVM.Entity.Validate();

            if (!PersonVM.Entity.IsValid())
            {
                ShowValidationTool(PersonVM.Error);
                return false;
            }
            else
            {
                try
                {
                    SetAccount();
                    PersonVM.Save();

                    UpdateAccounts();

                    if (vatError.Visibility == Visibility.Visible)
                        vatError.Visibility = Visibility.Collapsed;

                    return true;

                }
                catch (Exception ex)
                {
                    ShowValidationTool(ex.Message);
                    return false;
                }
            }
        }


        private void RibbonWindow_Loaded(object sender, RoutedEventArgs e)
        {
            dtpCreateDate.SelectedDate = DateTime.Now;
            cmbAccount.SelectedIndex = 0;
            cmbPersonType.SelectedIndex = 0;

            cmbRecord.IsEnabled = false;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            Save();
        }

        private void btnSaveAndNew_Click(object sender, RoutedEventArgs e)
        {
            if (Save())
                btnClear_Click(sender, e);
        }

        private void btnSaveAndClose_Click(object sender, RoutedEventArgs e)
        {
            if (Save())
                Close();
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            int selectedIndex = cmbPersonType.SelectedIndex;
            object selectedItem = cmbAccount.SelectedItem;

            PersonVM.Entity = new Person();

            if (selectedItem == Account.NullAccount || selectedItem == Account.NewAccount)
                cmbAccount.SelectedItem = selectedItem;
            else
                cmbAccount.SelectedIndex = 0;

            dtpCreateDate.SelectedDate = DateTime.Now;
            cmbPersonType.SelectedIndex = selectedIndex;

            BindingExpression d = contactD.GetBindingExpression(ContactDetailTool.ContactDetailsProperty);
            d.UpdateTarget();

            if (vatError.Visibility == Visibility.Visible)
                vatError.Visibility = Visibility.Collapsed;
        }

        private void btnRest_Click(object sender, RoutedEventArgs e)
        {
            btnClear_Click(sender, e);
        }

        private void btnSaveAndPrint_Click(object sender, RoutedEventArgs e)
        {
            PersonVM.Print();
        }

        private void btnPerviewPrint_Click(object sender, RoutedEventArgs e)
        {
            PersonVM.PreviewPrint();
        }

        private void btnSetupPrint_Click(object sender, RoutedEventArgs e)
        {
            PersonVM.PrintSettings();
        }

        private void btnLast_Click(object sender, RoutedEventArgs e)
        {
            PersonVM.Last();
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            PersonVM.Next();
        }

        private void btnPrevious_Click(object sender, RoutedEventArgs e)
        {
            PersonVM.Previous();
        }

        private void btnFirst_Click(object sender, RoutedEventArgs e)
        {
            PersonVM.First();
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void btnOptions_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
